package com.qclix.inv.webui;

import com.qclix.inv.Client;
import com.qclix.inv.Contract;
import com.qclix.inv.Invoice;
import com.qclix.inv.InvoiceLine;
import com.qclix.inv.webui.jsonobj.JSonClient;
import com.qclix.inv.webui.jsonobj.JSonContract;
import com.qclix.inv.webui.jsonobj.JSonInvoice;
import com.qclix.inv.webui.jsonobj.JSonInvoiceLine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Pierre Moermans
 */
public class JSonObjTranslator {
    public static JSonClient getJSon(Client model) {
        if (model == null)
            return null;
        return new JSonClient(
                model.getId(),
                model.getName(),
                model.getStreet1(),
                model.getZip(),
                model.getCity(),
                model.getCountry(),
                model.getVat());
    }

    public static Client getModel(JSonClient json) {
        if (json == null)
            return null;
        Client model = new Client();
        model.setId(json.getId());
        model.setName(json.getName());
        model.setStreet1(json.getStreet());
        model.setStreet2(null);
        model.setZip(json.getZip());
        model.setCity(json.getCity());
        model.setCountry(json.getCountry());
        model.setVat(json.getVat());
        return model;
    }

    public static JSonContract getJSon(Contract model) {
        if (model == null)
            return null;
        return new JSonContract(
                model.getReference(),
                getJSon(model.getClient()),
                model.getValidFrom(),
                model.getValidTill(),
                model.getHourlyRate());
    }

    public static Contract getModel(JSonContract json) {
        if (json == null)
            return null;
        Contract contract = new Contract();
        contract.setReference(json.getReference());
        contract.setClient(getModel(json.getClient()));
        contract.setValidFrom(json.getValidFrom());
        contract.setValidTill(json.getValidTill());
        contract.setHourlyRate(json.getHourlyRate());
        return contract;
    }

    public static JSonInvoice getJSon(Invoice model) {
        if (model == null)
            return null;
        JSonInvoice json = new JSonInvoice();
        json.setNumber(model.getNumber());
        json.setClient(getJSon(model.getClient()));
        json.setContract(getJSon(model.getContract()));
        json.setIssueDate(model.getIssueDate());
        List<JSonInvoiceLine> jsonLines = new ArrayList<JSonInvoiceLine>();
        int counter = 0;
        for (InvoiceLine lineModel : model.getLines()) {
            jsonLines.add(counter++, getJSon(lineModel));
        }
        json.setLines(jsonLines);
        return json;
    }

    public static Invoice getModel(JSonInvoice json) {
        if (json == null)
            return null;
        Invoice model = new Invoice();
        model.setNumber(json.getNumber());
        model.setIssueDate(json.getIssueDate());
        model.setClient(getModel(json.getClient()));
        model.setContract(getModel(json.getContract()));
        List<InvoiceLine> modelLines = new ArrayList<InvoiceLine>();
        int counter = 0;
        for (JSonInvoiceLine jsonLine : json.getLines()) {
            modelLines.add(counter++, getModel(jsonLine));
        }
        model.setLines(modelLines);
        return model;
    }

    public static JSonInvoiceLine getJSon(InvoiceLine model) {
        if (model == null)
            return null;
        JSonInvoiceLine json = new JSonInvoiceLine(model.getDescription(), model.getQuantity(), model.getUnitPrice(), model.getVat());
        json.setId(model.getId());
        return json;
    }

    public static InvoiceLine getModel(JSonInvoiceLine json) {
        if (json == null)
            return null;
        InvoiceLine model = new InvoiceLine(json.getDescription(), json.getQuantity(), json.getUnitPrice(), json.getVat());
        model.setId(json.getId());
        return model;
    }
}
